module.exports = (options, app) => {
  return async function jwt(ctx, next) {
    const authHeader = ctx.request.header.authorization;
    if (!authHeader || !authHeader.startsWith('Bearer ')) {
      ctx.status = 401;
      ctx.body = {
        code: 201,
        message: 'Authorization格式错误，需要Bearer token',
      };
      return;
    }
    const token = authHeader.substring(7); // 去掉'Bearer '前缀
    if (!token) {
      ctx.status = 401;
      ctx.body = {
        code: 201,
        message: '没有token，请先登录',
      };
      return;
    }

   
    console.log('Full app instance keys:', app);
    try {
       // console.log(token)
        const decoded = app.jwt.verify(token, app.config.jwt.secret);
      console.log(decoded)
      ctx.state.user = decoded;
      await next();
    } catch (err) {
        console.log(11,err)
      ctx.status = 401;
      ctx.body = {
        code: 201,
        message: 'token验证失败',
      };
    }
  };
};